﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class UIController : MonoBehaviour
{
	#region 单例模型
	private static UIController singleton;
	private UIController() { }      // 不允许外界创建对象
	private void Awake() { singleton = this; }
	public static UIController Singleton => singleton;
	#endregion

	#region  显示防御塔属性逻辑
	[Header("防御塔属性界面")]
	public GameObject infoPanel;
	[Header("等级")]
	public Text gradeText;
	[Header("攻击力")]
	public Text powerText;
	[Header("攻击范围")]
	public Text rangeText;
	[Header("攻击速度")]
	public Text speedText;
	// public bool IsShowInfo {get => infoPanel; }

	public void ShowInfo(Tower tower) {
		infoPanel.SetActive(true);
		gradeText.text = tower.grade.ToString();
		powerText.text = tower.power.ToString();
		rangeText.text = tower.range.ToString();
		speedText.text = tower.speed.ToString();
	}
	public void HideInfo() {
		infoPanel.SetActive(false);
	}
	#endregion

	#region PopOrPush创建塔界面
	[Header("选择防御塔界面")]
	public GameObject selectView;
	[Header("更新界面")]
	public GameObject updateView;
	[Header("Pop动画")]
	public Animator popPanelAnimator;
	/// <summary>
	/// 显示弹出框
	/// </summary>
	public void ShowPopPanel(UIState state) {
		popPanelAnimator.SetBool("IsShow", true);
		if (state == UIState.Select) {
			selectView.SetActive(true);
			updateView.SetActive(false);
		} else if (state == UIState.Update) {
			selectView.SetActive(false);
			updateView.SetActive(true);
			ShowInfo(camCon.target.GetComponent<TowerBase>().tower); // 显示属性信息
		}
	}

	/// <summary>
	/// 隐藏弹出框
	/// </summary>
	public void HidePopPanel() {
		popPanelAnimator.SetBool("IsShow", false);
	}
	#endregion

	#region 数据统计UI

	[Header("消灭的怪物数量")]
	public Text dieEnemyCountText;
	// public string DieEnemyCountText { set => dieEnemyCountText.text = value; }
	[Header("总的怪物数量")]
	public Text enemyCountText;
	[Header("玩家血量数量")]
	public Text bloodText;
	[Header("金币数量")]
	public Text coinText;

	public void SetDieEnemyCount(int count) {
		dieEnemyCountText.text = count.ToString();
	}
	public void SetEnemyCount(int count) {
		enemyCountText.text = count.ToString();
	}
	public void SetBlood(int bloodValue) {
		bloodText.text = bloodValue.ToString();
	}
	public void SetCoin(int coin) {
		coinText.text = coin.ToString();
	}
	#endregion

	#region 开始游戏的一些UI变化
	[Header("开始游戏前的CoverPanel")]
	private GameObject coverPanel;
	/// <summary>
	/// BeginGameBtn点击才可以创建怪物移动视野等等
	/// </summary>
	public void BeginGameClick(GameObject panel) {
		panel.SetActive(false);
		gameController.BeginGame();
	}

	public void ShowCoverPanel() {
		coverPanel.SetActive(true);
	}
	#endregion

	#region  结束界面的弹出
	[Header("结束界面")]
	public GameObject resultPnl;

	public void ShowResultPnl() {
		resultPnl.SetActive(true);
	}

	public void ResetGameClick() {
		gameController.ResetGame();
	}

	public Image[] stars = new Image[3];
	/// <summary>
	/// 展示星星
	/// </summary>
	/// <param name="num"></param>
	public void ShowStar(int num) {
		Sprite gray = stars[2].sprite;
		Sprite gold = stars[0].sprite;

		for (int i = 0; i < num; i++)
			stars[i].sprite = gold;
		for (int i = num; i < stars.Length; i++)
			stars[i].sprite = gray;
	}

	#endregion

	#region 升级防御塔

	public void UpdateTower() {
		TowerBase towerBase = camCon.target.GetComponent<TowerBase>();
		towCon.UpdateTower(towerBase);
	}

	public void DeleteTower() {
		TowerBase towerBase = camCon.target.GetComponent<TowerBase>();
		towCon.DeleteTower(towerBase);
		ShowPopPanel(UIState.Select);
	}

	#endregion

	#region TOAST调用

	[Header("吐司预制物")]
	public Toast toastPrefab;

	/// <summary>
	/// 创建吐司信息
	/// </summary>
	/// <param name="mag"></param>
	public void CreateToast(string msg) {
		Toast toast = Instantiate(toastPrefab, transform);
		toast.SetMessage(msg);
	}

	#endregion


	private TowerController towCon;   // TowerController的单例
	private CameraController camCon;
	private GameController gameController;
	public bool isUI;    // 鼠标是否在UI上面
	private void Start() {
		towCon = TowerController.Singleton;
		camCon = CameraController.Singleton;
		gameController = GameController.Singleton;
		coverPanel = transform.Find("CoverPanel").gameObject;
	}
	private void Update() {
		isUI = EventSystem.current.IsPointerOverGameObject();
	}

	public void OnTowerSelectClick(int index) {
		Transform target = camCon.target;
		if (target) towCon.CreateTower((TowerType)index, target);
	}
}
